<template>
  <div class="layout">
    <Layout>
      <Sider hide-trigger :style="{background: '#fff'}">
        <div class="search">
          <Input
            search
            enter-button="查找部门"
            placeholder="查找..."
            v-model="searchValue"
            @on-search="tryOpen"
          />
          <br />
          <!--<ul>是有序列表，<ol>是无序列表-->
          <ul class="search_menu" ref="search_menu">
            <li v-for="item in searchList" :key="item" @click="selectItem(item)">
              <i>{{item.searchname}}</i>
            </li>
          </ul>
        </div>
        <Tree
          ref="mytree"
          :data="dataTree"
          @on-select-change="selectByTree()"
          style="font-size:16px;font-weight: bold"
        ></Tree>
      </Sider>
      <Content :style="{padding: '24px', background: '#fff',minHeight: '240px'}">
        <div>
          <div style="margin-bottom:24px">
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="6">
                <Button
                  type="primary"
                  icon="ios-add-circle-outline"
                  style="margin:20px 0 0 0"
                  @click="openCreateDepart=true"
                >创建子部门</Button>
              </Col>
              <Col span="6">
                <Button
                  type="error"
                  icon="ios-close-circle-outline"
                  style="margin:20px 0 0 0"
                  @click="resure.deleteDepartment=true"
                >删除部门</Button>
              </Col>
            </Row>
          </div>
          <Modal v-model="openCreateDepart" title="创建部门" footer-hide>
            <div slot="header" style="font-size:14px;fontWeight:bold">
              <span>创建部门</span>
            </div>
            <div class="center">
              <Form
                ref="createDepartInfo"
                :model="createDepartInfo"
                :rules="createRules"
                :label-width="80"
                style="width:350px"
              >
                <FormItem label="上级部门" prop="radio">
                  <RadioGroup v-model="createDepartInfo.radio">
                    <Radio label="yes">以该部门为上级部门</Radio>
                    <Radio label="no">无须上级部门</Radio>
                  </RadioGroup>
                </FormItem>
                <FormItem label="部门编号" prop="number">
                  <Input v-model="createDepartInfo.number" placeholder="请输入部门编号"></Input>
                </FormItem>
                <FormItem label="部门名称" prop="name">
                  <Input v-model="createDepartInfo.name" placeholder="请输入部门名称"></Input>
                </FormItem>
                <FormItem label="联系电话">
                  <Input v-model="createDepartInfo.phone" placeholder="请输入联系电话"></Input>
                </FormItem>
                <FormItem label="部门传真">
                  <Input v-model="createDepartInfo.fax" placeholder="请输入传真号码"></Input>
                </FormItem>
                <FormItem label="部门类别">
                  <Input v-model="createDepartInfo.type" placeholder="请输入部门类别"></Input>
                </FormItem>
                <FormItem label="部门地区">
                  <Select v-model="createDepartInfo.areaName">
                    <Option
                      v-for="item in provinceList"
                      :value="item.value"
                      :key="item.value"
                    >{{ item.label }}</Option>
                  </Select>
                </FormItem>
                <FormItem label="所在区域">
                  <Input v-model="createDepartInfo.address" type="textarea" />
                </FormItem>
                <div class="center">
                  <Button type="primary" @click="createOK">创建</Button>
                </div>
              </Form>
            </div>
          </Modal>
          <Modal v-model="resure.deleteDepartment" titile="确认删除部门" @on-ok="submitDeleteDepartment">
            <div slot="header" style="font-size:14px;fontWeight:bold">
              <span>确认删除部门</span>
            </div>
            <div class="center warningInfo">
                  <Icon type="ios-alert" class="warnIcon"/>确认删除该部门吗
            </div>
          </Modal>
        </div>
        <Collapse v-model="value" simple>
          <!--                        部门信息-->
          <Panel name="information" :model="information" ref="information">
            <Icon type="md-clipboard" />
            <span style="font-size:13px;font-weight:bold">部门信息</span>
            <!--slot插槽，子组件的显示由父组件决定-->
            <div slot="content">
              <Form :label-width="80" inline>
                <FormItem label="部门名称">
                  <Input v-model="information.name" :readonly="read" />
                </FormItem>
                <FormItem label="部门类别">
                  <Input v-model="information.type" :readonly="read" />
                </FormItem>
                <FormItem label="部门编号">
                  <Input v-model="information.number" :readonly="read" />
                </FormItem>
                <FormItem label="创建日期">
                  <Input v-model="information.createDate" :readonly="read" />
                </FormItem>
                <FormItem label="部门区域">
                  <Input v-model="information.areaName" :readonly="read" />
                </FormItem>
                <FormItem label="联系电话">
                  <Input v-model="information.phone" :readonly="read" />
                </FormItem>
                <FormItem label="部门传真">
                  <Input v-model="information.fax" :readonly="read" />
                </FormItem>
                <FormItem label="所在区域">
                  <Input v-model="information.address" type="textarea" :readonly="read" />
                </FormItem>
                <FormItem>
                  <Button
                    @click="doEdit"
                    type="primary"
                    icon="ios-create-outline"
                    style="margin-right:40px"
                  >编辑</Button>
                </FormItem>
              </Form>
            </div>
            <Modal v-model="edit" title="编辑部门信息" footer-hide>
              <div slot="header" style="font-size:14px;fontWeight:bold">
                <span>编辑部门信息</span>
              </div>
              <div class="center">
                <Form
                  ref="editInformation"
                  :model="editInformation"
                  :rules="editRules"
                  :label-width="80"
                  style="width:350px"
                >
                  <FormItem label="部门名称" prop="name">
                    <Input v-model="editInformation.name" placeholder="请输入部门名称"></Input>
                  </FormItem>
                  <FormItem label="部门编号" prop="number">
                    <Input v-model="editInformation.number" placeholder="请输入部门编号"></Input>
                  </FormItem>
                  <FormItem label="部门类别" prop>
                    <Input v-model="editInformation.type" placeholder="请输入部门类别"></Input>
                  </FormItem>

                  <FormItem label="创建日期" prop>
                    <Date-picker
                      style="width:100%"
                      v-model="editInformation.createDate"
                      type="date"
                      placeholder="选择日期"
                      placement="bottom"
                    ></Date-picker>
                  </FormItem>
                  <FormItem label="部门地区" prop>
                    <!-- <Input v-model="editInformation.areaName" type="text" /> -->
                    <Select v-model="editInformation.areaName">
                      <Option
                        v-for="item in provinceList"
                        :value="item.value"
                        :key="item.value"
                      >{{ item.label }}</Option>
                    </Select>
                  </FormItem>
                  <FormItem label="联系电话" prop>
                    <Input v-model="editInformation.phone" type="text" />
                  </FormItem>
                  <FormItem label="部门传真" prop>
                    <Input v-model="editInformation.fax" type="text" />
                  </FormItem>
                  <FormItem label="所在区域" prop>
                    <Input v-model="editInformation.address" type="textarea" />
                  </FormItem>
                  <div class="center">
                    <Button type="primary" @click="editOk">确认</Button>
                  </div>
                </Form>
              </div>
            </Modal>
          </Panel>

        </Collapse>
        <Collapse v-model="value2" simple>
          <!--                        部门员工-->
          <Panel name="worker">
            <Icon type="md-clipboard" />
            <span style="font-size:13px;font-weight:bold">部门人员</span>
            <div slot="content">
              <div class="putRight">
                <Button
                  @click="openDeleteWorkers=true"
                  type="error"
                  icon="ios-close-circle-outline"
                  style="margin-right:40px"
                >批量删除员工</Button>
                <Button
                  @click="doAdd"
                  type="primary"
                  icon="ios-add-circle-outline"
                  style="margin-right:40px"
                >添加员工</Button>
              </div>
              <br />
              <i-table
                border
                stripe
                content="self"
                :columns="columns"
                :data="table"
                size="large"
                @on-selection-change="deleteWorkers"
              />
              <br />
              <Page
                :total="pageTotal"
                :current="pageNum"
                :page-size="pageSize"
                show-elevator
                show-total
                placement="top"
                @on-change="handlePage"
              ></Page>
            </div>
            <Modal v-model="resure.forbid" title="确认禁用" @on-ok="submitForbid">
              <div slot="header" style="font-size:14px;fontWeight:bold">
                <span>确认禁用</span>
              </div>
                  <div class="center warningInfo">
                        <Icon type="ios-alert" class="warnIcon"/>确认禁用该员工吗
                  </div>
            </Modal>
            <Modal v-model="resure.delete" titile="确认删除" @on-ok="submitFDelete">
              <div slot="header" style="font-size:14px;fontWeight:bold">
                <span>确认删除</span>
              </div>
            <div class="center warningInfo">
                  <Icon type="ios-alert" class="warnIcon"/>确认删除该员工吗
            </div>
            </Modal>
            <Modal v-model="openDeleteWorkers" titile="确认删除" @on-ok="submitDeleteWorkers">
              <div slot="header" style="font-size:14px;fontWeight:bold">
                <span>确认删除</span>
              </div>
            <div class="center warningInfo">
                  <Icon type="ios-alert" class="warnIcon"/>确认删除选中的员工吗
            </div>
            </Modal>
            <Modal v-model="resure.minister" titile="确认设为部长" @on-ok="submitMinister">
              <div slot="header" style="font-size:14px;fontWeight:bold">
                <span>确认设为部长</span>
              </div>
            <div class="center warningInfo">
                  <Icon type="ios-alert" class="warnIcon"/>确认设置该员工为部长吗
            </div>
            </Modal>
            <Modal v-model="resure.exempt" titile="确认撤销部长" @on-ok="submitExempt">
              <div slot="header" style="font-size:14px;fontWeight:bold">
                <span>确认撤销部长</span>
              </div>
            <div class="center warningInfo">
                  <Icon type="ios-alert" class="warnIcon"/>确认撤销该员工的部长职位吗
            </div>
            </Modal>
            <Modal v-model="add" title="添加部门员工" width="700px" footer-hide>
              <div slot="header" style="font-size:14px;fontWeight:bold">
                <span>添加部门员工</span>
              </div>
              <div class="right" style="margin-bottom:10px">
                <Button
                  type="primary"
                  style="margin-right:40px"
                  @click="resure.add=true"
                  icon="ios-add-circle-outline"
                >添加</Button>
                <Input
                  search
                  enter-button="搜索"
                  v-model="searchKey"
                  style="width:200px"
                  @on-search="searchOutWorker"
                />
              </div>
              <i-table
                border
                stripe
                content="self"
                :columns="addColumns"
                :data="addTable"
                size="large"
                @on-selection-change="addWorkers"
              />
              <br />
              <Modal v-model="resure.add" title="确认添加" @on-ok="submitAddWorder">
                <div slot="header" style="font-size:14px;fontWeight:bold">
                  <span>确认添加</span>
                </div>
                  <div class="center warningInfo">
                        <Icon type="ios-alert" class="warnIcon"/>确认添加这些员工吗
                  </div>
              </Modal>
              <Page
                :total="addPageTotal"
                :current="addPageNum"
                :page-size="addPageSize"
                show-elevator
                show-total
                placement="top"
                @on-change="addHandlePage"
              ></Page>
            </Modal>
          </Panel>
        </Collapse>
      </Content>
    </Layout>
  </div>
</template>/>
<script>
import AreaList from "../../assets/area";
export default {
  data() {
    //检查输入的内容是否为空、含有空格或者含有数字
    const validateInput = (rule, value, callback) => {
      const res = this.common.judgeInput(value);
      if (res === -1) {
        callback(new Error("输入只能是汉字或英文符号"));
      } else {
        callback();
      }
    };
    //检查输入的编号是否为空、含有空格
    const validateNumberInput = (rule, value, callback) => {
      const res = this.common.judgeNumberInput(value);
      if (res === -1) {
        callback(new Error("输入只能是汉字、英文或数字符号"));
      } else {
        callback();
      }
    };
    return {
      //创建部门的信息
      // createRadio:true,
      createDepartInfo: {
        radio: "yes",
        parentId: null,
        number: "",
        name: "",
        phone: "",
        fax: "",
        areaName: "",
        address: ""
      },
      submitCreateInfo: {
        parentId: null,
        number: "",
        name: "",
        phone: "",
        fax: "",
        areaName: "",
        address: ""
      },
      //创建部门的表单验证
      createRules: {
        radio: [
          { required: true, message: "请选择是否有父级目录", trigger: "blur" }
        ],
        number: [
          { required: true, message: "请输入部门编号", trigger: "blur" },
          { validator: validateNumberInput, trigger: "blur" }
        ],
        name: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
          { validator: validateInput, trigger: "blur" }
        ]
      },
      areaList: AreaList,
      provinceList: [],
      /**初始化部门信息 */
      information: {
        id: 1,
        parentId: "",
        type: "",
        name: "",
        number: "",
        areaName: "",
        phone: "",
        fax: "",
        address: "",
        createDate: ""
      },
      editInformation: {
        // id: 1,
        type: "",
        name: "",
        number: "",
        createDate: "",
        areaName: "",
        phone: "",
        fax: "",
        address: ""
      },
      editRules: {
        number: [
          { required: true, message: "部门编号不能为空", trigger: "blur" },
          { validator: validateNumberInput, trigger: "blur" }
        ],
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          { validator: validateInput, trigger: "blur" }
        ]
      },
      /**初始化部门员工表的page属性 */
      pageTotal: 0,
      pageNum: 1,
      pageSize: 5,
      /**初始化添加员工里面员工表的page属性 */
      addPageTotal: 0,
      addPageNum: 1,
      addPageSize: 3,
      /**给readonly绑定true */
      read: true,
      /**add绑定 */
      /**edit绑定编辑部门信息的modal，最开始不显示 */
      edit: false,
      /**add绑定添加员工时的弹窗 */
      add: false,
      /**resur.add绑定添加员工，
      * resure.forbid绑定禁用员工的提示框，
      * resure.delete绑定删除员工，
      * resure.minister绑定设为部长的提示框,
      * resue_deletedepartment绑定删除部门,
      * resue_exempt绑定撤销部长的提示框*/
      resure: {
        add: false,
        forbid: false,
        delete: false,
        minister: false,
        deleteDepartment: false,
      exempt:false
      },
      /**row绑定部门员工信息点击每一行的操作的行信息 */
      row: {},
      /**value绑定collapse默认打开的面板，这里默认打开部门信息 */
      value: "information",
      value2:"worker",
      /**searchValue绑定搜索框用户输入的值 */
      searchValue: "",
      /**searchKey绑定添加员工的弹窗中的搜索框里用户的输入值 */
      searchKey: "",
      searchList: [],
      dataTree: [],
      /**columns储存部门员工信息的表头 */
      columns: [
        {
          type: "selection",
          width: 50
        },
        {
          title: "状态",
          width: 65,
          key: "stats",
          /**渲染一个现实员工是否禁用的图标 ,其中deleteTag为1时表示禁用*/
          render: (h, params) => {
            var text = "";
            if (params.row.deleteTag == true) {
              text = "禁用状态";
            } else {
              text = "非禁用状态";
            }
            return h("div", [
              h(
                "Tooltip",
                {
                  props: {
                    placement: "bottom",
                    content: text,
                    transfer: true
                  }
                },
                [
                  h("Icon", {
                    props: {
                      type: params.row.deleteTag
                        ? "ios-close-circle"
                        : "md-checkmark-circle"
                    },
                    style: {
                      fontSize: "18px",
                      color: params.row.deleteTag ? "#f88081" : "#58c38b"
                    }
                  })
                ]
              )
            ]);
          }
        },
        {
          title: "员工编号",
          key: "number",
          minWidth: 120
        },
        {
          title: "姓名",
          key: "name",
          minWidth: 120,
          //用颜色改变名字区分部长和普通员工
          render: (h, params) => {
            var minister = "";
            if (params.row.ministerTag == true) {
              minister = "部长";
            } else {
              minister = "非部长";
            }
            return h(
              "Tooltip",
              {
                props: {
                  placement: "bottom",
                  content: minister,
                  transfer: true
                }
              },
              [
                h(
                  "span",
                  {
                    style: {
                      color: params.row.ministerTag ? "#CD1076" : "black"
                    }
                  },
                  params.row.name
                )
              ]
            );
          }
        },
        {
          title: "性别",
          key: "sex",
         width: 65
        },
        {
          title: "职位",
          key: "position",
          minWidth: 120
        },
        {
          title: "电话",
          key: "phone",
         minWidth: 130
        },
        {
          title: "项目负责人",
          key: "isleader",
          minWidth: 120
        },
        {
          title: "环评工程师",
          key: "isengineer",
          minWidth: 120
        },
        {
          title: "负责行业类别",
          key: "industryName",
          minWidth: 140
        },
        {
          title: "操作",
          key: "action",
       width: 100,
          fixed: "right",
          render: (h, params) => {
            return h(
                 "Dropdown",
                {
                  on: {
                    "on-click": name => {
                      this.showAction(name, params.row);
                    }
                  },
                  props: {
                    transfer: true
                  }
                },
                [
                  h(
                    "Button",
                    {
                      props: {
                        type: "primary"
                      }
                    },
                    "操作"
                  ),
                  h(
                    "DropdownMenu",
                    {
                      slot: "list"
                    },
                    [
                      h(
                        "DropdownItem",
                        {
                          props: {
                            name: "forbid",
                            disabled: params.row.deleteTag
                          }
                        },
                        "禁用员工"
                      ),
                      h(
                        "DropdownItem",
                        {
                          props: {
                            name: "delete"
                          }
                        },
                        "删除员工"
                      ),
                      h(
                        "DropdownItem",
                        {
                          props: {
                            name: "minister",
                            disabled: params.row.ministerTag
                          }
                        },
                        "设为部长"
                      ),
                        h(
                        "DropdownItem",
                        {
                          props: {
                            name: "exempt",
                            disabled: !params.row.ministerTag
                          }
                        },
                        "撤销部长"
                      ),

                    ]
                  )
                ]
            )
         }
       }
      ],
 
      /**table储存部门员工信息 */
      table: [],
      /** addColumns储存添加员工时的员工表信息表头*/
      addColumns: [
        {
          type: "selection",
          width: 50,
      //    fixed: 'left',
        },
        {
          title: "员工编号",
          key: "number",
            minWidth:120,
                  //     fixed: 'left',
        },
        {
          title: "姓名",
          key: "name",
           minWidth:120
        },
        {
          title: "性别",
          key: "sex",
            width:65
        },
        {
          title: "职位",
          key: "position",
            minWidth:120

        },
        {
          title: "电话",
          key: "phone",
           minWidth:130
        },
        {
          title: "项目负责人",
          key: "isleader",
           minWidth:120
        },
        {
          title: "环评工程师",
          key: "isengineer",
            minWidth:120
        },
        {
          title: "负责行业类别",
          key: "industryName",
            minWidth:140
        }
      ],
      /** addTable储存添加员工时的部门员工信息*/
      addTable: [],
      /**addList中存放最终要加入部门的员工的id */
      addList: [],
      /**存放最终要删除的员工的id*/
      deleteList: [],
      //绑定确认是否批量删除员工的窗口
      openDeleteWorkers: false,
      //绑定确认创建部门的窗口
      openCreateDepart: false
    };
  },
  beforeMount() {
    this.getCityData();
    /**获取部门信息树 */
    this.getDepartTree();
  },
  methods: {
    //获取部门信息树
    getDepartTree() {
      this.apis.departmentApis.getDepartments(res => {
        var code = res.code;
        if (code === 0) {
          var data = res.data;
          this.dataTree = [];
          var i = 0;
          for (i = 0; i < data.length; i++) {
            this.preOrder(data[i]);
          }
          //将改完索引后的data赋给dataTree
          this.dataTree = data;

          /**默认刷新页面后显示总部的信息,部门id为第一个*/
          if (this.dataTree.length !== 0) {
            this.information.id = this.dataTree[0].id;
            this.pageNum = 1;
            this.submitDepartment(this.information.id);
            this.submitWorker();
          }
        }
      });
    },
    //获取省级地区信息
    getCityData() {
      var province = this.areaList.province_list;
      var item;

      for (item in province) {
        this.provinceList.push({
          value: province[item],
          label: province[item]
        });
      }
    },
    //先序遍历一下后端传过来的树，将里面的前端需要的三个字段取一下改一下索引，改为tree控件可以用的形式
    preOrder(node) {
      if (node != null) {
        // this.dataTree.push()
        node.title = node.nodeDepartmentIndex.name;
        node.id = node.nodeDepartmentIndex.id;
        node.children = node.childrenDepartmentIndexes;
        var i = 0;
        for (i = 0; i < node.childrenDepartmentIndexes.length; i++) {
          this.preOrder(node.childrenDepartmentIndexes[i]);
        }
      }
    },
    //切换页面的方法
    handlePage(value) {
      this.pageNum = value;
      this.submitWorker();
    },
    //添加员工里员工表的切换页面方法
    addHandlePage(value) {
      this.addPageNum = value;
      this.submitOutWorker();
    },
    //请求得到部门信息的方法
    submitDepartment(id) {
      this.apis.departmentApis.searchForDepartmentById(id, res => {
        var code = res.code;
        if (code === 0) {
          this.information = res.data;
        }
      });
    },
    //请求得到部门人员信息，同时将后端传来的布尔值改为前端可以显示的中文
    submitWorker() {
      this.apis.departmentApis.getDepartmentsConsistent(
        this.information.id,
        this.pageNum,
        this.pageSize,
        res => {
          var code = res.code;
          if (code == 0) {
            var list = res.data.list;
            var total = res.data.total;
            var i = 0;
            for (i = 0; i < list.length; i++) {
              if (list[i].gender == true) {
                list[i].sex = "女";
              } else if (list[i].gender == false) {
                list[i].sex = "男";
              }
              if (list[i].leaderTag == true) {
                list[i].isleader = "是";
              } else if (list[i].leaderTag == false) {
                list[i].isleader = "否";
              }
              if (list[i].registeredEIAEngineerTag == true) {
                list[i].isengineer = "是";
              } else if (list[i].registeredEIAEngineerTag == false) {
                list[i].isengineer = "否";
              }
            }
            this.table = list;
            this.pageTotal = total;
          } else {
            this.table = "";
            this.pageTotal = 0;
          }
        }
      );
    },
    //根据用户每次按下键盘获取输入内容发给后端进行模糊搜索
    searchInfo() {
      this.apis.departmentApis.searchForDepartmentName(
        this.searchValue,
        res => {
          var code = res.code;
          if (code == 0) {
            var list = res.data;
            var i = 0;
            for (i = 0; i < list.length; i++) {
              if (list[i].name != null && list[i].parentName != null) {
                list[i].searchname = list[i].parentName + "->" + list[i].name;
              } else if (list[i].parentName == null) {
                list[i].searchname = list[i].name;
              }
            }
          } else {
            list = "";
          }
          this.searchList = list;
        }
      );
    },
    //当用户点击搜索或者回车时，弹出提示框
    tryOpen() {
      this.searchInfo();
      //要获取一个元素需要用ref属性，而不是通过document
      this.$refs.search_menu.style.display = "block";
    },
    //关闭提示框
    tryClose() {
      this.$refs.search_menu.style.display = "none";
    },
    //通过select方法实现选中提示框的内容后填充到Input框中，并且把值传递给后端
    selectItem(item) {
      this.information.id = item.id;
      this.searchValue = item.searchname;
      this.pageNum = 1;
      this.submitDepartment(this.information.id);
      this.submitWorker();
      this.tryClose();
    },
    //通过树形部门信息选择部门时请求后端数据的方法
    selectByTree() {
      //获取选择的节点
      var node = this.$refs.mytree.getSelectedNodes()[0];
      this.information.id = node.id;
      this.pageNum = 1;
      this.submitDepartment(this.information.id);
      this.submitWorker();
    },
    //点击编辑部门信息时弹出窗口
    doEdit() {
      //这里不使用直接索引复制，否则会导致部门信息和编辑部门信息中的值指向同一个地址
      //this.editInformation.id=this.information.id;
      this.editInformation.type = this.information.type;
      this.editInformation.name = this.information.name;
      this.editInformation.number = this.information.number;

      this.editInformation.createDate = this.information.createDate;
      this.editInformation.areaName = this.information.areaName;
      this.editInformation.phone = this.information.phone;
      this.editInformation.fax = this.information.fax;
      this.editInformation.address = this.information.address;
      this.edit = true;
    },
    DateToString(date) {
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      if (month < 10) {
        var newmonth = "0" + month;
        month = newmonth;
      }
      if (day < 10) {
        var newday = "0" + day;
        day = newday;
      }
      var sDate = year + "-" + month + "-" + day;

      return sDate;
    },
    //编辑部门信息时点击确定的回调
    editOk() {
      this.$refs["editInformation"].validate(valid => {
        if (valid) {
          this.information.type = this.editInformation.type;
          this.information.name = this.editInformation.name;
          this.information.number = this.editInformation.number;
          //将date类型转为String类型后再传回给后端
          //this.information.createDate=this.editInformation.createDate;
          if (this.editInformation.createDate != this.information.createDate) {
            this.information.createDate = this.DateToString(
              this.editInformation.createDate
            );
          } else {
            this.information.createDate = this.editInformation.createDate;
          }
          this.information.areaName = this.editInformation.areaName;
          this.information.phone = this.editInformation.phone;
          this.information.fax = this.editInformation.fax;
          this.information.address = this.editInformation.address;
          this.submitEdit();
        }
      });
    },
    //提交编辑部门信息的请求
    submitEdit() {
      let data = {
        id: this.information.id,
        number: this.information.number,
        name: this.information.name,
        phone: this.information.phone,
        fax: this.information.fax,
        areaName: this.information.areaName,
        address: this.information.address,
        parentId: this.information.parentId,
        type: this.information.type,
        createDate: this.information.createDate
      };
      this.apis.departmentApis.editDepartment(
        this.information.id,
        data,
        res => {
          var code = res.code;
          if (code == 0) {
            this.$Message.success(res.data);
          } else {
            this.$Message.error(res.msg);
          }
        }
      );
    },
    //点击添加员工弹出窗口，并且请求后端得到没加入部门的员工信息
    doAdd() {
      this.addPageNum = 1;
      this.searchKey = "";
      this.submitOutWorker();
      this.add = true;
    },
    /**添加员工的弹窗中实现搜索未加入部门的员工 */
    searchOutWorker() {
      this.addPageNum = 1;
      this.submitOutWorker();
    },

    submitOutWorker() {
      this.apis.departmentApis.searchPeopleOutOfDepartmentByKey(
        this.addPageNum,
        this.addPageSize,
        this.searchKey,
        res => {
      console.log("添加员工信息：",res)
          var code = res.code;
          if (code == 0) {
            var list = res.data.list;
            var total = res.data.total;
            var i = 0;
            for (i = 0; i < list.length; i++) {
              if (list[i].gender == true) {
                list[i].sex = "女";
              } else if (list[i].gender == false) {
                list[i].sex = "男";
              }
              if (list[i].leaderTag == true) {
                list[i].isleader = "是";
              } else if (list[i].leaderTag == false) {
                list[i].isleader = "否";
              }
              if (list[i].registeredEIAEngineerTag == true) {
                list[i].isengineer = "是";
              } else if (list[i].registeredEIAEngineerTag == false) {
                list[i].isengineer = "否";
              }
            }
            this.addTable = list;
            this.addPageTotal = total;
          } else {
            this.addTable = "";
            this.addPageTotal = 0;
          }
        }
      );
    },
    //每次勾选复选框时就计算一次被选中的员工，记录在要提交给后端的数组中
    addWorkers(selection) {
      var i = 0;
      this.addList = [];

      for (i = 0; i < selection.length; i++) {
        this.addList.push(selection[i].id);
      }
    },
    //选择确定添加员工的回调,提交添加员工的请求
    submitAddWorder() {
      //当被添加的员工非空的时候才提交请求
      if (!(this.addList.length == 0)) {
        this.apis.departmentApis.addPeopleToDepartment(
          this.information.id,
          this.addList,
          res => {
            var code = res.code;
            if (code == 0) {
              this.$Message.success(res.data);
              this.pageNum = 1;
              this.addPageNum = 1;
              this.submitWorker();
              this.searchOutWorker();
            } else {
              this.$Message.error(res.msg);
            }
          }
        );
        //每次提交后又将添加员工的数组置为空
        this.addList = [];
      } else {
        this.$Message.warning("请至少选择一位员工进行添加！");
      }
    },
    //接收处理部门员工的三个操作
    showAction(name, row) {
      this.row = row;
      if (name == "forbid") {
        this.resure.forbid = true;
      } else if (name == "delete") {
        this.resure.delete = true;
      } else if (name == "minister") {
        this.resure.minister = true;
      }else if(name=="exempt"){
        this.resure.exempt=true;
      }
    },
    //用户点击确认禁用后提交禁用员工的请求
    submitForbid() {
      this.apis.departmentApis.shutPersonInDepartment(
        this.information.id,
        this.row.id,
        res => {
          var code = res.code;
          if (code == 0) {
            this.$Message.success(res.data);
            this.pageNum = 1;
            this.submitWorker();
          } else {
            this.$Message.error(res.msg);
          }
        }
      );
      this.row = {};
    },
    //用户点击确认删除后提交删除员工的请求
    submitFDelete() {
      this.apis.departmentApis.deletePersonFromDepartment(
        this.information.id,
        this.row.id,
        res => {
          var code = res.code;
          if (code == 0) {
            this.$Message.success(res.data);
            this.pageNum = 1;
            this.submitWorker();
          } else {
            this.$Message.error(res.msg);
          }
        }
      );
      this.row = {};
    },
    //用户点击选择框加入要删除的员工
    deleteWorkers(selection) {
      var i = 0;
      this.deleteList = [];

      for (i = 0; i < selection.length; i++) {
        this.deleteList.push(selection[i].id);
      }
    },

    //提交批量删除员工的请求
    submitDeleteWorkers() {
      if (this.deleteList.length > 0) {
        this.apis.departmentApis.deletePersonsFromDepartment(
          this.information.id,
          this.deleteList,
          res => {
            var code = res.code;
            if (code == 0) {
              this.$Message.success(res.data);
              this.pageNum = 1;
              this.submitWorker();
            } else {
              this.$Message.error(res.msg);
            }
          }
        );
        this.row = {};
      } else {
        this.$Message.warning("请至少选择一位员工进行删除！");
      }
    },
    //用户点击确认设为部长后提交设为部长的请求
    submitMinister() {
      this.apis.departmentApis.setPersonToBeLeader(
        this.information.id,
        this.row.id,
        res => {
          var code = res.code;
          if (code == 0) {
            this.$Message.success(res.data);
            this.pageNum = 1;
            this.submitWorker();
          } else {
            this.$Message.error(res.msg);
          }
        }
      );
      this.row = {};
    },
      //用户点击确认撤销部长的请求
      submitExempt(){
      this.apis.departmentApis.exemptLeader(this.information.id,this.row.id,res=>{
            var code = res.code;
          if (code == 0) {
            this.$Message.success(res.data);
            this.pageNum = 1;
            this.submitWorker();
          } else {
            this.$Message.error(res.msg);
          }
      })
      },
    //创建部门确认
    createOK() {
      this.$refs["createDepartInfo"].validate(valid => {
        if (valid) {
          if (this.createDepartInfo.radio === "yes") {
            this.createDepartInfo.parentId = this.information.id;
          }
          this.apis.departmentApis.createDepartment(
            this.createDepartInfo,
            res => {
              var code = res.code;
              if (code === 0) {
                this.$Message.success(res.data);
                this.getDepartTree();
                this.openCreateDepart = false;
                /**暂不支持默认刷新页面后显示新建部门，因为新建的部门id未知*/
                //  this.information.id=this.createDepartInfo.number;
                this.submitDepartment(this.information.id);
                this.submitWorker();
              }
            }
          );
        }
        this.createDepartInfo = {
          radio: "yes",
          parentId: null,
          number: "",
          name: "",
          phone: "",
          fax: "",
          areaName: "",
          address: ""
        };
      });
    },

    //用户点击确定删除部门后删除该部门
    submitDeleteDepartment() {
      this.apis.departmentApis.deleteDepartment(this.information.id, res => {
        var code = res.code;
        if (code === 0) {
          this.$Message.success(res.data);
          this.getDepartTree();

          this.information.id = this.dataTree[0].id;
          this.submitDepartment(this.information.id);
          this.submitWorker();
        } else {
          this.$Message.error(res.msg);
        }
      });
    }
  }
};
</script>
<style scoped>
@import "../../../static/global.css"; /*引入公共样式*/
.putRight {
  display: flex;
  justify-content: flex-end;
  align-content: center;
}


</style>